package com.ca.ui.core.view.card;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.Html;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.ca.ui.core.R;
import com.ca.ui.core.bean.MiniComment;
import com.ca.ui.core.util.NumberUtils;

public class MiniCommentCard extends ConstraintLayout {

    private MiniComment comment;

    public TextView supportCountView;
    public TextView contentView;

    public MiniCommentCard(@NonNull Context context) {
        super(context);
        initView(context, null);
    }

    public MiniCommentCard(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public MiniCommentCard(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    private void initView(Context context, AttributeSet attrs) {

        View view = LayoutInflater.from(context).inflate(R.layout.mini_comment_card_layout, this);
        supportCountView = view.findViewById(R.id.tv_supportCount);
        contentView = view.findViewById(R.id.tv_content);

        comment = new MiniComment();
        if (attrs != null) {
            TypedArray array = context.getTheme().obtainStyledAttributes(attrs, R.styleable.MiniCommentCard, 0, 0);

            comment.setSupportCount((long) array.getInteger(R.styleable.MiniCommentCard_mcc_supportCount, 0));
            comment.setName(array.getString(R.styleable.MiniCommentCard_mcc_name));
            comment.setContent(array.getString(R.styleable.MiniCommentCard_mcc_content));

            array.recycle();
        }

        setComment(comment);

        setOnClickListener(v -> {});
    }

    public MiniComment getComment() {
        return comment;
    }

    public void setComment(MiniComment comment) {

        this.comment = comment;

        if (comment == null) {
            setVisibility(GONE);
            return;
        }

        setVisibility(VISIBLE);

        String text = NumberUtils.numberToString(comment.getSupportCount()) + "赞";
        supportCountView.setText(text);
        supportCountView.setVisibility(comment.getSupportCount() == 0 ? GONE : VISIBLE);

        text = "<font color='#109D58'>" + comment.getName() + "</font>: " + comment.getContent();
        contentView.setText(Html.fromHtml(text));
    }
}
